$(document).ready(prt_getPRTList);

/* Gets PRT list */
function prt_getPRTList()
{
  $.getJSON("php/VehicleList.php", function(jsonData)
  {
    $.each(jsonData, function(key,value)
    {
      $('#prt_selectedVehicle').append($("<option></option>").attr("value",value.vehicleId).text('Araç ' + value.vehicleId));
    });
  });
}

/* When a prt is selected, get information about that prt */
var selectedVehicleId;
function prt_getInformation()
{
  $('#prt_info_state, #prt_info_freeSeats, #prt_info_battery, #prt_info_destination, #prt_info_speed, #prt_info_location, #prt_info_totalDistance').empty();

  selectedVehicleId = document.getElementById("prt_selectedVehicle").value;

  var xmlhttp = new XMLHttpRequest();
  var url = "php/Vehicles.php/" + selectedVehicleId;
  xmlhttp.open("GET",url,false);
  xmlhttp.send();
  var json = xmlhttp.responseText; 
  obj = JSON.parse(json);

  for(var count = 0; count< obj.length; count++)
  {
    document.getElementById("prt_info_state").innerHTML = obj[count].status;
    document.getElementById("prt_info_freeSeats").innerHTML = obj[count].freeSeats;
    document.getElementById("prt_info_battery").innerHTML = obj[count].battery;
    document.getElementById("prt_info_destination").innerHTML = obj[count].destination;
    document.getElementById("prt_info_speed").innerHTML = obj[count].speed;
    document.getElementById("prt_info_location").innerHTML = obj[count].position;
    document.getElementById("prt_info_totalDistance").innerHTML = obj[count].distanceTravelled;
  }

  prt_plot();
}

//PRT Command Functions
function prt_start(){

  $("#loader_prtCommand").show();
  $.get('http://' + HOST_URL + ':85/'+selectedVehicleId+'/web_go').success(
    function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarılı");
    })
  .error(function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarısız");
  });
}


function prt_stop(){

  $("#loader_prtCommand").show();
  $.get('http://' + HOST_URL + ':85/'+selectedVehicleId+'/web_stop').success(
    function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarılı");
    })
  .error(function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarısız");
  });
}

function prt_sensor_start(){

  $("#loader_prtCommand").show();
  $.get('http://' + HOST_URL + ':85/'+selectedVehicleId+'/web_status').success(
    function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarılı");
    })
  .error(function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarısız");
  });
}

  
function prt_charge(){
  $("#loader_prtCommand").show();
  $.get('http://' + HOST_URL + ':85/'+selectedVehicleId+'/web_recharge').success(
    function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarılı");
    })
  .error(function() {
      $("#loader_prtCommand").delay(200).fadeOut('fast');
      alert("Komut gönderme başarısız");
  });
}